﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using Project.MVC.DataAccess;
using Project.MVC.Admin.Util;
using Project.MVC.Admin.Models;
namespace Project.MVC.Admin.Controllers
{
    public class NewsController : Controller
    {
        NewAccess _newAccess = new NewAccess();
        CategoryAccess _categoryAccess = new CategoryAccess();
        /// <summary>
        ///Giá trị default type của News là tin tức 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public ActionResult Index(int id = 1, int type = 2)
        {
            var model = new NewsModel();
            model.Type = type;
            model.PageList = _newAccess.GetList().ToPagedList(id, 5);
            return View("Index", model);
        }

        public ActionResult Add(int type)
        {
            var model = new NewsModel();
            model.Type = type;
            model.New = new New();
            model.ParentCategories = _categoryAccess.GetAllCategories(false, type).ToList();
            return View("Add", model);
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Add(FormCollection form, int type, NewsModel model)
        {
            model = GetModel(form, 2, true);
            _newAccess.Insert(model.New);
            return View("Edit", model);
        }

        public ActionResult Edit(int id)
        {
            var model = _newAccess.GetByKey(id);
            return View("Edit", model);
        }

        [HttpPost]
        public ActionResult Edit(FormCollection form)
        {
            var model = new New();
            return View("Edit", model);
        }

        public void Delte(int id)
        {
            var model = new New();
            model.Id = id;
            _newAccess.Delete(model);
            RedirectToAction("Index");
        }
        /// <summary>
        /// Hiển thị thông báo khi lưu hoặc thêm mới dữ liệu thành công
        /// </summary>
        /// <returns></returns>
        protected string ShowMessage(string message)
        {
            string s = string.Empty;
            s += "<dl id=\"system-message\">";
            s += "<dd class=\"message message\"><ul>";
            s += "<li>" + message + "</li>";
            s += "</ul></dd>";
            s += "</dl>";
            return s;
        }
        protected NewsModel GetModel(FormCollection form, int type, bool isNew)
        {
            var model =new NewsModel();

            model.New.Title = form.Get("txtTitle");
            model.New.ShortDescription = form.Get("txtShortDescription");
            model.New.Description = form.Get("txtDescription");
            model.New.Image = form.Get("txtImageUrl");
            //model.New.IsHot = bool.Parse(form.Get("cbHot"));
            //model.New.IsHome = bool.Parse(form.Get("cbHome"));
            return model;
        }
    }
}
